#ifndef AFE_CMD_H_
#define AFE_CMD_H_

#include "spi_dev_ctl.h"
#include <stdint.h>

#define AFE_CH_0 ((uint16_t)0x0001)   /*!< CH 0 selected */
#define AFE_CH_1 ((uint16_t)0x0002)   /*!< CH 1 selected */
#define AFE_CH_2 ((uint16_t)0x0004)   /*!< CH 2 selected */
#define AFE_CH_3 ((uint16_t)0x0008)   /*!< CH 3 selected */
#define AFE_CH_4 ((uint16_t)0x0010)   /*!< CH 4 selected */
#define AFE_CH_5 ((uint16_t)0x0020)   /*!< CH 5 selected */
#define AFE_CH_6 ((uint16_t)0x0040)   /*!< CH 6 selected */
#define AFE_CH_7 ((uint16_t)0x0080)   /*!< CH 7 selected */
#define AFE_CH_8 ((uint16_t)0x0100)   /*!< CH 8 selected */
#define AFE_CH_9 ((uint16_t)0x0200)   /*!< CH 9 selected */
#define AFE_CH_10 ((uint16_t)0x0400)  /*!< CH 10 selected */
#define AFE_CH_11 ((uint16_t)0x0800)  /*!< CH 11 selected */
#define AFE_CH_12 ((uint16_t)0x1000)  /*!< CH 12 selected */
#define AFE_CH_13 ((uint16_t)0x2000)  /*!< CH 13 selected */
#define AFE_CH_14 ((uint16_t)0x4000)  /*!< CH 14 selected */
#define AFE_CH_15 ((uint16_t)0x8000)  /*!< CH 15 selected */
#define AFE_CH_ALL ((uint16_t)0xFFFF) /*!< All CH selected */

#define AFE_SPD_0 ((uint16_t)0x0001)
#define AFE_SPD_1 ((uint16_t)0x0002)
#define AFE_SPD_ALL ((uint16_t)0x0003) //! 双转速通道

typedef enum
{
    PGA_GAIN_DIV_8 = '0',   /*!< pga gain: 1/8 */
    PGA_GAIN_DIV_4 = '1',   /*!< pga gain: 1/4 */
    PGA_GAIN_DIV_2 = '2',   /*!< pga gain: 1/2 */
    PGA_GAIN_DIV_1 = '3',   /*!< pga gain: 1 */
    PGA_GAIN_MUL_2 = '4',   /*!< pga gain: 2 */
    PGA_GAIN_MUL_4 = '5',   /*!< pga gain: 4 */
    PGA_GAIN_MUL_8 = '6',   /*!< pga gain: 8 */
    PGA_GAIN_MUL_16 = '7',  /*!< pga gain: 16 */
    PGA_GAIN_MUL_32 = '8',  /*!< pga gain: 32 */
    PGA_GAIN_MUL_64 = '9',  /*!< pga gain: 64 */
    PGA_GAIN_MUL_128 = ':', /*!< pga gain: 128 */
} pga_gain_t;

typedef enum
{
    CHX_MODE_DEF = '0',  /*!< default mode */
    CHX_MODE_IEPE = '1', /*!< iepe acc mode */
    CHX_MODE_DC = '2',   /*!< dc mode */
    CHX_MODE_AC = '3',   /*!< ac mode */
    CHX_MODE_TIB = '4',  /*!< test mode */
    CHX_MODE_CAL = '5',  /*!< calibration mode */
} chx_mode_t;

typedef enum
{
    SPDX_MODE_DEF = '0', /*!< default mode */
    SPDX_MODE_MAG = '1', /*!< megnetic speed mode */
    SPDX_MODE_OPT = '2', /*!< optics speed mode */
    SPDX_MODE_TIB = '3', /*!< test in board speed mode */
} spdx_mode_t;

typedef enum
{
    SAMP_25K = '0',  /*!< default 25K SAMPRATE */
    SAMP_50K = '1',  /*!< 50K SAMPRATE */
    SAMP_100K = '2', /*!< 100K SAMPRATE */
    SAMP_200K = '3', /*!< 200K SAMPRATE */
} samp_rate_t;

typedef enum
{
    AFE_CHX_NUM_6 = 6,
    AFE_CHX_NUM_8 = 8,
    AFE_CHX_NUM_16 = 16,
    AFE_CHX_NUM_32 = 32,
} chx_num_t;

typedef enum
{
    AFE_SPDX_NUM_1 = 1,
    AFE_SPDX_NUM_2 = 2,
    AFE_SPDX_NUM_4 = 4,
} spdx_num_t;

typedef struct afe_dev_s
{
    chx_num_t chx_num;
    spdx_num_t spdx_num;
    spi_dev_t *spi;
    int (*send)(const uint8_t cmd_code[], uint8_t ret_code[]); // set
} afe_dev_t;

int afe_acq_begin(afe_dev_t *afe_dev);
int afe_acq_stop(afe_dev_t *afe_dev);
int afe_cal_begin(afe_dev_t *afe_dev);
int afe_cal_stop(afe_dev_t *afe_dev);
int afe_get_fw(afe_dev_t *afe_dev, uint8_t afe_fw[]);
int afe_pwr_off(afe_dev_t *afe_dev);
int afe_set_chx_gain(afe_dev_t *afe_dev, uint16_t chx, pga_gain_t gain);
int afe_get_chx_gain(afe_dev_t *afe_dev, uint16_t chx, pga_gain_t *gain);
int afe_set_chx_mode(afe_dev_t *afe_dev, uint16_t chx, chx_mode_t mode);
int afe_get_chx_mode(afe_dev_t *afe_dev, uint16_t chx, chx_mode_t *mode);
int afe_set_spdx_mode(afe_dev_t *afe_dev, uint16_t spdx, spdx_mode_t mode);
int afe_get_spdx_mode(afe_dev_t *afe_dev, uint16_t spdx, spdx_mode_t *mode);
int afe_set_samp_rate(afe_dev_t *afe_dev, samp_rate_t samp_rate);
int afe_get_samp_rate(afe_dev_t *afe_dev, samp_rate_t *samp_rate);
int afe_get_samp_rate(afe_dev_t *afe_dev, samp_rate_t *samp_rate);
int afe_get_stat(afe_dev_t *afe_dev, uint8_t afe_stat[]);

#endif
